package core.world;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Vector;
import java.util.regex.PatternSyntaxException;

import core.GameOptions;

public class MapFileReader {	
	
	static int mapSizeMultiplyer = 100000;
	static Map map;
	
	public static void loadFile(String fileName, Map mapToModify){
		
		
			
		map=mapToModify;
		
		try{
			Scanner sc = new Scanner( new FileReader(GameOptions.mapFolder+fileName));
			while(sc.hasNext()){
				String line = sc.nextLine();
				handle(line);
				
			}
			
			
		}
		catch(FileNotFoundException ex){
			System.out.println("Map file " +fileName+ " not found");
		}
		
	}
	private static void handle(String line){
		Scanner s = new Scanner(line);
		Vector<String> input = new Vector<String>();
		while(s.hasNext()){
			input.add(s.next());
		}
		String command = input.get(0);
		//Following calls sends the entire command.
		if(command.equals("sizeMultiplyer"))  // sizeMultiplyer 100000
			sizeMultiplyer(input);
		if(command.equals("wall"))			// wall (4x14) (8x15) (17x4)    //CounterClockwise
			wall(input);
		if(command.equals("spawn"))			// spawn (4x14) (8x15) (17x4)
			spawn(input);
		if(command.equals("point"))			// point name (4x14) (8x15) (17x4)
			point(input);
		if(command.equals("cornerWalls"))			// cornerWalls 20
			cornerWalls(input);
		
			
	}
	
	private static void wall(Vector<String> input){
		Vector<Point> polygon = new Vector<Point>();
		for(String s : input){
			Point p = toPoint(s);
			if(p != null){
				polygon.add(p);
			}
		}
		WallGroup wg = new WallGroup(polygon);
		map.addWallGroup(wg);
	}
	private static void spawn(Vector<String> input){
		for(String s : input){
			Point p = toPoint(s);
			if(p != null){
				map.addSpawn(p);
			}
		}
	}
	private static void point(Vector<String> input){
		String pointName = input.get(1);
		Vector<Point> points = new Vector<Point>();
		for(String s : input){
			Point p = toPoint(s);
			if(p != null){
				points.add(p);
			}
		}
		map.addPointGroup(pointName, points);
	}
	private static void sizeMultiplyer(Vector<String> input){
		mapSizeMultiplyer = Integer.parseInt(input.get(1));
	}
	private static void cornerWalls(Vector<String> input){
		map.addCornerWalls(Integer.parseInt(input.get(1)) * mapSizeMultiplyer);
	}
	/**
	 * returns a point at location given by ps * mapSizeMultiplyer
	 * @param ps formatted (xxy) , ie (13x16)
	 * @return
	 */
	private static Point toPoint(String ps){
		Scanner s = new Scanner(ps);
		int x, y;
		try{
			int i=0, j=0;
			if(ps.charAt(0) == '('){
				while(ps.charAt(i) != 'x')
					i++;
			}
			//System.out.println("Parsing: "+ps);
			String sub1 = ps.substring(1, i);
			String sub2 = ps.substring(i+1, ps.length()-1);
			x=Integer.parseInt(sub1)*mapSizeMultiplyer;
			y=Integer.parseInt(sub2)*mapSizeMultiplyer;
			
			//System.out.println(x+" "+ y);
			return new Point(x, y);

		}
		catch(InputMismatchException ex){
			
			return null;
		}
		catch(PatternSyntaxException ex2){
			return null;
		}
		catch(StringIndexOutOfBoundsException ex3){
			return null;
		}
	}
	
}









